package com.llxz.blog.basic.exceptionhandler;

import com.llxz.blog.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by IntelliJ IDEA.
 *
 * @author zzh
 * @description:. date: 2021/9/4 9:54
 */

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    //1.全局异常
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R error(Exception e){
        log.error(e.getMessage());
        e.printStackTrace();
        System.out.println(e.toString());
        return R.error().message("全局异常");
    }
    //3，自定义异常
    //3，自定义异常
    @ExceptionHandler(MyException.class)
    @ResponseBody
    public R error(MyException e){
        log.error(e.getMessage());
        e.printStackTrace();

        return R.error().code(e.getCode()).message(e.getMessage());
    }
}
